package javabase.schoolwork.day6_15;

import org.junit.Test;

import java.io.InputStream;
import java.lang.reflect.*;

/**
 * @author fance
 * @date 18-6-16 上午6:35
 */
public class Sum {

    public void fun1() {
        Class<?> c1 = null;
        Class<?> c2;
        Class<?> c3;

        try {
            c1 = Class.forName("javabase.schoolwork.day6_15.Sum");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        c2 = new Sum().getClass();
        c3 = Sum.class;

        System.out.println(c1.getName());
        System.out.println(c2.getName());
        System.out.println(c3.getName());
    }
    public void classLoader() throws ClassNotFoundException {
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
/*
        System.out.println(classLoader);

        classLoader = classLoader.getParent();
        System.out.println(classLoader);
        classLoader = classLoader.getParent();
        System.out.println(classLoader);

        classLoader = Class.forName("javabase.schoolwork.day6_16.Sum").getClassLoader();
        System.out.println(classLoader);*/



        classLoader = Class.forName("java.lang.Object").getClassLoader();
        System.out.println(classLoader);

        InputStream inputStream =
                this.getClass().getClassLoader().getResourceAsStream("javabase/schoolwork/day6_15/test.properties");
        System.out.println(inputStream);
    }

    public void getSuperClass(Class<?> clazz) {
        Class parent = clazz.getSuperclass();
        System.out.println(parent.getName());
        if (!parent.getName().equals("java.lang.Object")) {
            getSuperClass(parent);
        }

    }

    public void constructor() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
        Class<?> c1 = Class.forName("javabase.schoolwork.day6_15.Person");

        Constructor[] constructors = c1.getConstructors();
        for (int i = 0; i < constructors.length; i++) {
            System.out.println(constructors[i]);
        }

       // Class<?> person = Class.forName("javabase.schoolwork.day6_15.Person");

        Person p = (Person) constructors[1].newInstance("fance",5);
       /* p.setName("fance");
        p.setAge(3)*/
        System.out.println(p);
    }
    public void interfaces() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
        Class<?> c1 = Class.forName("javabase.schoolwork.day6_15.Person");

       /* Class[] classes = c1.getInterfaces();
        for (int i = 0; i < classes.length; i++) {
            System.out.println(classes[i].getName());
        }
        System.out.println(c1.getSuperclass().getName());
  */

        /*Method method = c1.getMethod("sayChina");
        method.invoke(c1.newInstance());*/
        Method method = c1.getMethod("sayHello", String.class, int.class);
        System.out.println(method.invoke(c1.newInstance(),"fance",21));
    }


    public void person() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException {
        Class<?> c = Class.forName("javabase.schoolwork.day6_15.Person");

        Person p = (Person) c.newInstance();


        Field nameField = c.getDeclaredField("name");
        Field ageField = c.getDeclaredField("age");

          nameField.setAccessible(true);
        nameField.set(p,"fance");
          ageField.setAccessible(true);
        ageField.set(p,20);

        System.out.println(nameField.get(p));
        System.out.println(ageField.get(p));
    }


    public void arr(){
        int[] tmp = {1,2,3};
        Class c = tmp.getClass().getComponentType();
        System.out.println(c.getName());
        System.out.println(Array.getLength(tmp));
        System.out.println(Array.get(tmp,0));
        Array.set(tmp,0,6);
        System.out.println(Array.get(tmp,0));
    }

    public void arr2() {
        int[] tmp = {1,2,3};
        int[] newTmp = (int[]) arrayInc(tmp,5);
        print(newTmp);

        System.out.println("-------");
        print(tmp);
    }
    private Object arrayInc(Object obj,int len) {
        Class c = obj.getClass();
        Class arr = c.getComponentType();
        Object newO = Array.newInstance(arr,len);
        System.arraycopy(obj,0,newO,0,Array.getLength(obj));

        return newO;
    }
    private void print(Object obj) {
        Class<?> c = obj.getClass();
        if (!c.isArray()) {
            return;
        }

        Class arr = c.getComponentType();
        System.out.println(arr.getName() + " " + Array.getLength(obj));
        for (int i = 0; i < Array.getLength(obj); i++) {
            System.out.println(Array.get(obj,i));
        }
    }

    public void invocationHandler() {
        Person person = new Person();
        MyInvocationHandler handler =
                new MyInvocationHandler(person);

        China china = (China) Proxy.newProxyInstance(person.getClass().getClassLoader(),
                person.getClass().getInterfaces(),
                handler);
        System.out.println(china.sayHello("fance",21));
    }
    @Test
    public void test() throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, NoSuchFieldException {
//        fun1();

//        classLoader();

//        getSuperClass("java.lang.String");
//        constructor();

//        interfaces();
//        person();
//        arr();
//        arr2();
        invocationHandler();
    }
}
